<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
				xmlns:fo="http://www.w3.org/1999/XSL/Format" 
				xmlns:xs="http://www.w3.org/2001/XMLSchema" 
				xmlns:fn="http://www.w3.org/2005/xpath-functions" >
	<xsl:param name="dir" />
	<xsl:param name="last-level" select="1"/>
	<xsl:output method="xml" omit-xml-declaration="yes" use-character-maps="style"/>
	<!--
		Why? Because you can't put a xsl:processing-instruction inside a xsl:attriute
	-->
	<xsl:character-map name="style">
		<xsl:output-character character="«" string="&lt;?"/>
		<xsl:output-character character="»" string="?&gt;"/>
	</xsl:character-map>
	<xsl:template match="/">
		<xsl:result-document href="./{$dir}/menu.php">
			<div class="side_menu">
				<h1 style="color:green;">DDI</h1>
				<h2><span style="color:blue">Code</span><span style="color:magenta;margin-left:-0.1em;">r</span> 2.0</h2>
				<div class="menu">
				<xsl:apply-templates select="//*:CodeScheme">
					<xsl:with-param name="level">0</xsl:with-param>
				</xsl:apply-templates>
				</div>
			</div>
		</xsl:result-document>
	</xsl:template>
	<xsl:template match="*:CodeScheme">
		<xsl:apply-templates select="*:Label" />
		<div class="sidemenu_level_1"><ul>
		<xsl:apply-templates select="*:Code">
			<xsl:with-param name="level" select="2" />
			<xsl:with-param name="last-level" select="$last-level" />
		</xsl:apply-templates>
		</ul></div>
	</xsl:template>
	<xsl:template match="*:Label">
		<xsl:value-of select="."/>
	</xsl:template>
	<xsl:template match="*:Code">
		<xsl:param name="level"/>
		<xsl:param name="last-level"/>
		<xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/*:ID"/>
		</xsl:variable>
		<li>
			<xsl:if test="$last-level&gt;=$level">
				<xsl:element name="a">
					<xsl:attribute name="href">#</xsl:attribute>
					<xsl:attribute name="class">showhide_link</xsl:attribute>
					<xsl:attribute name="onclick">
						$('#submenu_of_<xsl:value-of select="$categoryID"/>').toggle();
						$(this).children('span').toggle();
					</xsl:attribute>
					<xsl:processing-instruction name="php">
						include("./style/show-hide.php")
					</xsl:processing-instruction>
	
				</xsl:element>
			</xsl:if>
			<xsl:element name="a">
				<!-- This is sooooo ugly, but to dynamically know where we are in the filesystem we *need* $backTrackDepth, and xsl:attr only allows characters -->
				<xsl:attribute name="href">./«php echo $backTrackDepth;»<xsl:for-each select="ancestor::*:Code"><xsl:value-of select="*:CategoryReference/*:ID"/>/</xsl:for-each><xsl:value-of select="$categoryID"/>.php</xsl:attribute>
				<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
			</xsl:element>
			<xsl:if test="$last-level&gt;=$level">
				<div class="sidemenu_level_{$level}" id="submenu_of_{$categoryID}"><ul>
				<xsl:apply-templates select="*:Code">
					<xsl:with-param name="level" select="$level+1" />
					<xsl:with-param name="last-level" select="$last-level" />
				</xsl:apply-templates>
				</ul></div>
			</xsl:if>
		</li>
	</xsl:template>
</xsl:stylesheet>
